package com.example.e_comm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.mapper.PermissionMapper;
import com.example.e_comm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper mapper;

    @Override
    public List<Permission> getPermissionsByPage(Page<Permission> page) {
        return mapper.selectList(page,null);
    }

    @Override
    public IPage<Permission> getPermissionsByPageWithCondition(Page<Permission> page, String keyword) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank( keyword)){
            wrapper.like("perm_name",keyword)
                    .or().like("perm_key",keyword);
        }
        return mapper.selectPage(page,wrapper);
    }

    @Override
    public List<Permission> getPermissionsByUserId(Long userId) {
        return mapper.getPermissionsByUserId(userId);

    }

    @Override
    public List<Permission> getPermissionsByRoleId(Long roleId) {
        return mapper.getPermissionsByRoleId(roleId);
    }

    @Override
    public Set<String> getPermissionsByUserIdSet(Long userId) {
        List<Permission> list = mapper.getPermissionsByUserId(userId);
        return list.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    @Override
    public Set<String> getPermissionsByRoleIdSet(Long roleId) {
        List<Permission> list = mapper.getPermissionsByRoleId(roleId);
        return list.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
//        return null;
    }
}
